package com.gr.dmbackend.service.Impl.sysadmin;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gr.dmbackend.mapper.BuildingMapper;
import com.gr.dmbackend.model.Building;
import com.gr.dmbackend.service.sysadmin.GetBuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GetBuildingServiceImpl implements GetBuildingService {

    @Autowired
    private BuildingMapper buildingMapper;

    @Override
    public JSONObject getBuilding(int page) {

        // 实现分页，IPage是mybatisplus的配置
        IPage<Building> recordIPage = new Page<>(page, 10);   // 第一个参数是第几页，第二个参数是每一页里面有多少个项目
        QueryWrapper<Building> queryWrapper =new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        List<Building> records = buildingMapper.selectPage(recordIPage, queryWrapper).getRecords();
        JSONObject resp = new JSONObject();
        resp.put("records", records);
        resp.put("records_count", buildingMapper.selectCount(queryWrapper));

        return resp;
    }
}
